package com.Image.util;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;

public class CdnUtils {
	public static String sendFile(File file, boolean is) {
		List<String> filePaths = new ArrayList<>();
		String fileName = UUID.randomUUID().toString().replace("-", "") + ".jpg";
		File file2 = new File(file.getParentFile(), fileName);
		forImage(file, file2);
		filePaths.add(file2.getAbsolutePath());
		if (is) {
			filePaths.add(getThumbImage.thumbnailImage(file2, 750, getHight.getH(file, 750), "XG-750-", false)
					.getAbsolutePath());
			filePaths.add(getThumbImage.thumbnailImage(file2, 500, getHight.getH(file, 500), "XG-500-", false)
					.getAbsolutePath());
		}
		for (String filePath : filePaths) {
			// 上传到linux服务器
			Connection conn = new Connection("47.93.252.8", 22);
			try {
				conn.connect();
				boolean isAuthenticated = conn.authenticateWithPassword("root", "sxs@5168");
				if (isAuthenticated == false) {
					System.err.println(".................authentication failed.......");
				}
				SCPClient client = new SCPClient(conn);
				client.put(filePath, "/home/work/local/wwwroot/images");
				conn.close();
				new File(filePath).delete();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return fileName;
	}
    //copay文件
	private static void forImage(File f1, File f2) {
		 FileInputStream inputStream = null;
         BufferedOutputStream stream=null;
          try { 
         inputStream = new FileInputStream(f1);
         int i = inputStream.available();
         // byte数组用于存放图片字节数据
         byte[] buff = new byte[i];
          inputStream.read(buff);
        // 记得关闭输入流
         inputStream.close();
        // 存入本地
        FileOutputStream fstream = new FileOutputStream(f2);
        stream = new BufferedOutputStream(fstream);
        stream.write(buff);
        stream.close();
       } catch (Exception e) {
         e.printStackTrace();  
      } 
   
	}

}
